package com.wjm.socket;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private EditText et_ip;
    private EditText et_port;
    private Button btn_start_socket;
    private TextView tv_serversocket_result;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById();
    }

    private void findViewById() {
        et_ip = findViewById(R.id.et_ip);
        et_port = findViewById(R.id.et_port);
        btn_start_socket = findViewById(R.id.btn_start_socket);
        tv_serversocket_result = findViewById(R.id.tv_serversocket_result);
        iv = findViewById(R.id.iv);
        btn_start_socket.setOnClickListener(v -> {
            tv_serversocket_result.setText("开始连接...");
            startSocket();
        });

        String url = "http://ww1.sinaimg.cn/large/0073sXn7ly1ft82s05kpaj30j50pjq9v.jpg";

//        Glide.with(this).load(url).into(iv);

        GlideApp.with(this)
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .fitCenter()
                .into(iv);
    }

    private void startSocket() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    startSocketTest();
                } catch (IOException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String date0 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()).toString();
                            tv_serversocket_result.setText(date0+"e.toString()==>" + e.toString());
                        }
                    });
                }
            }

            private void startSocketTest() throws IOException {
//                String ip = "192.168.176.1";//192.168.176.1
//                String ip = "192.168.0.101";
//                String ip = "192.168.0.111";
//                String ip = "192.168.176.1";
                String ip = "192.168.176.1";
                //192.168.176.1
                //192.168.86.11
                ip = et_ip.getText().toString().trim();
                int port = 8888;
                port = Integer.parseInt(et_port.getText().toString().trim());
                int timeout = 5000;
//                Socket s = new Socket(ip, port);//192.168.176.1
                Socket s = new Socket();//192.168.176.1
                s.connect(new InetSocketAddress(ip, port), timeout);
                s.setSoTimeout(timeout);
                // 判断客户端和服务器是否连接成功
                boolean connected = s.isConnected();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String date0 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()).toString();
                        tv_serversocket_result.setText(date0 + "connected==>" + connected);
                    }
                });
                OutputStream out = s.getOutputStream();
                InputStream is = s.getInputStream();
                byte[] buf = new byte[128];
                int len;
//        while ((len = is.read(buf)) != -1) {
                out.write("你好，我是客户端，收到请回答！".getBytes());
                out.flush();
                len = is.read(buf);
                String result = new String(buf, 0, len);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String date0 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()).toString();
                        tv_serversocket_result.setText(date0+"result==>" + result);
                    }
                });
                String date0 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()).toString();
                System.out.println(date0+result);
//        }
                out.close();
                is.close();
                s.close();
            }
        }).start();
    }

}
